package net.utils;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.enums.Enum;

public class CommonUtils {

	public static final String DATE_FORMAT_WITH_TIME = "yyyy-MM-dd HH:mm:ss";

	public static final String DATE_FORMAT_NO_TIME = "yyyy-MM-dd";

	public static String getExpressionVaue(String expression, String startTage,
			String endTag, Object bean) throws Exception {

		String[] properties = StringUtils.substringsBetween(expression,
				startTage, endTag);

		if (ArrayUtils.isNotEmpty(properties)) {

			for (int i = 0; i < properties.length; i++) {
				Object value = PropertyUtils.getProperty(bean, properties[i]);
				if (value != null) {
					expression = StringUtils.replace(expression, startTage
							+ properties[i] + endTag, String.valueOf(value));
				}
			}
		}

		return expression;
	}

	public static String getTimePeriod(long timeInSeconds) {

		StringBuffer period = new StringBuffer();

		long hours = timeInSeconds / 3600;

		long days = hours / 24;

		if (days > 0) {
			period.append(days).append("天");
		}

		timeInSeconds = timeInSeconds - (hours * 3600);

		hours = hours - days * 24;

		if (hours > 0) {
			period.append(hours).append("小时");
		}

		long minutes = timeInSeconds / 60;

		if (minutes > 0) {
			period.append(minutes).append("分钟");
		}

		timeInSeconds = timeInSeconds - (minutes * 60);

		long seconds = timeInSeconds;

		if (seconds > 0) {
			period.append(seconds).append("秒");
		}

		return period.toString();
	}

	public static String[] getEnumNames(Enum[] enums) {
		if (ArrayUtils.isEmpty(enums)) {
			return ArrayUtils.EMPTY_STRING_ARRAY;
		} else {
			String[] names = new String[enums.length];

			for (int i = 0; i < names.length; i++) {
				names[i] = enums[i].getName();

			}

			return names;
		}
	}
}
